<?php
namespace Admin\Controller;
use Admin\Model\BaseinfoVersionModel;
use Base\Controller\CurdController;
use Library\UploadFile;
use Library\Utils;
class MedicineinfoController extends CurdController
{
    public function __construct()
    {
        parent::__construct(true, true, array(
            'input_name' => 'coverFile',//文本框name
            'save_path' => 'covers',//保存路径
            'data_field' => 'cover'//数据库字段
        ));
    }

    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['name'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['name_en']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
            $where['status']=1;
        }else{
            $where['status']=0;
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        if($_REQUEST['selectes']){
                   $where['typeid']=$_REQUEST['selectes'];
        }
        $data = $this->model->pageToView($limit, true,$where);
//        echo    $this->model->_SQL();
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }
    public function add()
    {
//        if(empty($_POST['phone']) || empty($_POST['email'])){
//            $this->model->validate(false);
//        }
        $data = $this->model->create();
//        $this->error(json_encode($data));
//        dump($data);
//       return;
        if (!$data) {
            $this->error($this->model->getError());
        } else {
            //检查密码
//            $this->checkPwd();
            if ($this->upload) {
                //上传图片
                $input_name = $this->uploadConfig['input_name'];
                $save_path = $this->uploadConfig['save_path'];
                $data_field = $this->uploadConfig['data_field'];
                if ($_FILES[$input_name]) {
                    $up = new UploadFile();
                    $info = $up->upFile($save_path, array('jpg', 'png', 'jpeg', 'gif'));
                    $data[$data_field] = $info[$input_name]['savepath'] . $info[$input_name]['savename'];
                }
            }
            $data = $this->addAfter($data);
            $add = $this->model->add($data);
            $this->addEnd($add);
        }
    }
    protected function addAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
        if(empty($data['packages'])){
            $data['dose'] = $data['content'].$data['dimension'];
        }else{

            $data['dose'] = $data['content'].$data['dimension'].'/'.$data['packages'];
        }
        $data['typename'] = D('medicinetype')->where('id='.$data['typeid'])->getField('name');
        return $data;
    }
    protected function editAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        if(empty($data['packages'])){
            $data['dose'] = $data['content'].$data['dimension'];
        }else{

            $data['dose'] = $data['content'].$data['dimension'].'/'.$data['packages'];
        }
        $data['typename'] = D('medicinetype')->where('id='.$data['typeid'])->getField('name');
        return $data;
    }

} 